<?php
namespace App\Http\Controllers\Admin\Auth;

use App\Models\Admin\AdminLogModel;
use App\Services\Admin\AdminService;
use App\Utilities\JiugeForm;
use Illuminate\Http\Request;
use Hash;

class AdminController extends BaseController
{
    /**
     * 管理员
     */

    protected $view = 'admin.auth.admin.';//视图前缀
    protected $url = 'admin';//路由前缀
    protected $model;
    protected $formElementArr = [//表单元素
        //array(表单选项,中文名称,name字段名称,表单提示,js验证规则)
        array(1,'管理员','name','管理员，2-20字符，例：管理员',1),
    ];
    protected $fieldShowArr = [
        'id' => 'ID',
        'name' => '管理员',
    ];

    public function __construct()
    {
        parent::__construct();
        $this->prefix_url = $this->prefix_url.'/'.$this->url;
        $this->model = new AdminService();
        view()->share('prefix_url',$this->prefix_url);
        view()->share('jiugeForm',new JiugeForm($this->prefix_url));
    }

    public function index()
    {
        $datas = $this->model->getIndex();
        return view($this->view.'index',array(
            'datas' => $datas,
            'prefix_url' => $this->prefix_url,
        ));
    }

    public function create()
    {
        return view($this->formView.'edit',array(
            'sels' => $this->formElementArr,
            'options' => array(),
        ));
    }

    public function store(Request $request)
    {
        $data = $this->getData($request);
        $data['pwd'] = Hash::make('12345');
        $this->model->add($data);
        return redirect($this->prefix_url);
    }

    public function edit($id)
    {
        return view($this->formView.'edit',array(
            'sels' => $this->formElementArr,
            'options' => array(),
            'data' => $this->model->getOne($id),
        ));
    }

    public function update(Request $request,$id)
    {
        $data = $this->getData($request);
        $this->model->modify($data,$id);
        return redirect($this->prefix_url);
    }

    /**
     * 密码更新
     */
    public function setPwd(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->id;
            $pwd = $request->pwd;
            $pwd2 = $request->pwd2;
            $model = $this->model->getOne($id);
            if (!$model) {
                return array('code'=>-2,'msg'=>'管理员不存在！');
            }
            if (!(Hash::check($pwd,$model->pwd))) {
                return array('code'=>-3,'msg'=>'老密码错误！');
            }
            if (!preg_match('/[0-9a-zA-z-Z_]/',$pwd2)) {
                return array('code'=>-3,'msg'=>'新密码格式错误！');
            }
            $this->model->setPwd($id,Hash::make($pwd2));
            return array('code'=>0,'msg'=>'操作成功！');
        }
        return array('code'=>-1,'msg'=>'操作错误！');
    }

    /**
     * 密码重置
     */
    public function setPwd2(Request $request)
    {
        if ($request->ajax()) {
            $id = $request->id;
            $model = $this->model->getOne($id);
            if (!$model) {
                return array('code'=>-2,'msg'=>'管理员不存在！');
            }
            $this->model->setPwd($id,Hash::make('12345'));
            return array('code'=>0,'msg'=>'操作成功！');
        }
        return array('code'=>-1,'msg'=>'操作错误！');
    }

    public function getData(Request $request)
    {
        $name = $request->name;
        if (!$name) {
            echo "<script>alert('参数错误！');history.go(-1);</script>"; exit;
        }
        return array(
            'name' => $name,
        );
    }

    /**
     * 登陆记录
     */
    public function getLoginList()
    {
        $models = AdminLogModel::orderBy('id','desc')
            ->paginate(10);
        $models->limit = 10;
        return view('admin.auth.adminlog.index',array(
            'datas' => $models,
        ));
    }
}
